package com.duoduo.admin.controller;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.common.core.constant.CacheConstants;
import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.duoduo.admin.api.dto.SysDictItemDTO;
import com.duoduo.admin.service.SysDictItemService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("sysDictItem")
@Tag(name = "字典详情表接口", description ="字典详情表访问接口")
@RequiredArgsConstructor
public class SysDictItemController {
	
	private final SysDictItemService sysDictItemService;
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	public R<Page<SysDictItemDTO>> page(PageRequest<SysDictItemDTO> request){
		Page<SysDictItemDTO> page = sysDictItemService.page(request.getPage(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	
	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@GetMapping("/key/{key}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#key")
	public R<List<SysDictItemDTO>> getDictByKey(@PathVariable String key) {
		return R.ok(sysDictItemService.getDictByKey(key));
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	public R<SysDictItemDTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(sysDictItemService.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	public R<SysDictItemDTO> create(SysDictItemDTO request){
		sysDictItemService.create(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("update")
	public R<SysDictItemDTO> update(SysDictItemDTO request){
		sysDictItemService.update(request,request.getId());
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	public R<SysDictItemDTO> delete(@Parameter(description = "主键id") @PathVariable("id")Long id){
		sysDictItemService.delete(id);
		return R.ok();
	}
}
